
#include "StreamControlWidget.h"

StreamControlWidget::StreamControlWidget()
{
	m_nState = 0;

	// create interface
	createInterface();

	// connect signals
	connect(m_btnStart, SIGNAL(clicked()), this, SLOT(btnStart_Click()));
	connect(m_btnStop, SIGNAL(clicked()), this, SLOT(btnStop_Click()));
	connect(m_btnRefresh, SIGNAL(clicked()), this, SLOT(btnRefresh_Click()));
	connect(m_btnSetting, SIGNAL(clicked()), this, SLOT(btnSetting_Click()));

	// refresh slot list
	m_btnStart->setEnabled(false);
}

StreamControlWidget::~StreamControlWidget(void)
{
}

void StreamControlWidget::createInterface()
{
	m_btnStart = new QPushButton(tr(BUTTON_START));
	m_btnStop = new QPushButton(tr(BUTTON_STOP));
	m_btnStop->setEnabled(false);
	m_btnRefresh = new QPushButton(tr(BUTTON_REFRESH));
	m_btnSetting = new QPushButton(tr(BUTTON_SETTING));

	QHBoxLayout *pButtonLayout = new QHBoxLayout();
	pButtonLayout->addWidget(m_btnStart, Qt::AlignCenter);
	pButtonLayout->addWidget(m_btnStop, Qt::AlignCenter);
	pButtonLayout->addWidget(m_btnRefresh, Qt::AlignCenter);
	pButtonLayout->addWidget(m_btnSetting, Qt::AlignCenter);
	pButtonLayout->setContentsMargins(0, 0, 0, 0);
	pButtonLayout->setMargin(0);

	setLayout(pButtonLayout);
}

void StreamControlWidget::btnStart_Click()
{
	if (0 == m_nState) // stop
	{
		m_btnStop->setEnabled(true);
		m_btnRefresh->setEnabled(false);
		m_btnSetting->setEnabled(false);
		m_nState = 1;
		m_btnStart->setText(BUTTON_PAUSE); // Start -> Pause
		emit onStart();
	}
	else if (1 == m_nState) // start
	{
		m_nState = 2;
		m_btnStart->setText(BUTTON_RESUME); // Pause -> Resume
		emit onPause();
	}
	else if (2 == m_nState) // pause
	{
		m_nState = 1;
		m_btnStart->setText(BUTTON_PAUSE); // Resume -> Pause
		emit onResume();
	}
}

void StreamControlWidget::btnStop_Click()
{
	m_btnStop->setEnabled(false);
	m_btnRefresh->setEnabled(true);
	m_btnSetting->setEnabled(true);
	m_nState = 0;
	m_btnStart->setText(BUTTON_START); // set to Start
	emit onStop();
}

void StreamControlWidget::btnRefresh_Click()
{
	m_btnRefresh->setEnabled(false);
	updatesEnabled();

	emit onUpdate();

	m_btnRefresh->setEnabled(true);
}

void StreamControlWidget::btnSetting_Click()
{
	emit onShowSettings();
}

void StreamControlWidget::setReady(bool bReady)
{
	m_nState = 0;
	m_btnStart->setEnabled(bReady);
	m_btnStart->setText(BUTTON_START);
	m_btnStop->setEnabled(false);
	m_btnSetting->setEnabled(true);
	m_btnRefresh->setEnabled(true);
}